package gui;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

import model.Delivery;
import model.Estate;
import model.Road;
import model.RoadCollection;
import database.EstateOperations;
import database.RoadOperations;

import java.awt.*;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Vector;

@SuppressWarnings("serial")
public class tab2 extends JPanel implements ItemListener, ActionListener
{
	JPanel p1 ,p2;
	JButton print, estate, road,show;
	JComboBox <String> day, est,road1,est2;
	String days[] = {"Monday","Tuesday","Wednsday","Thursday","Friday","Saturday","Sunday"};
	private ArrayList<String> ests;
	private String estName;
	private String dayPicked;
	private EstateOperations eo;
	private RoadOperations ro;
	public static JTable table;
	public static DefaultTableModel model;
	
	public tab2()  
	{
		this.setPreferredSize(new Dimension(795, 395));
		this.setLayout(new FlowLayout());
		eo=new EstateOperations();
		ro=new RoadOperations();
		
		//Panel 1 buttons and combo s
		p1 = new JPanel();
		p1.setLayout(new FlowLayout());
		p1.setPreferredSize(new Dimension(795, 32));
		
		//list of days in combo box
		day = new JComboBox<String>(days);
		day.setPreferredSize(new Dimension(100,25));
		day.addItemListener(this);
		dayPicked=(String)day.getSelectedItem();
		
		//list of estates in combo box
		est = new JComboBox<String>();	
		est.setPreferredSize(new Dimension(100,25));
		ests=new ArrayList<String>();
		ests=eo.getEstateNames();
		for(int i=0;i<ests.size();i++)
		{
			est.addItem(ests.get(i));
		}
		est.addItemListener(this);
		estName=(String)est.getSelectedItem();
		
		show=new JButton("SHOW");
		show.addActionListener(this);
		
		print = new JButton("PRINT");
		print.addActionListener(this);
		estate = new JButton("Add Estate");
		estate.addActionListener(this);
		road = new JButton("Add Road");
		road.addActionListener(this);
		
		p1.add(day);
		p1.add(est);
		p1.add(show);
		p1.add(Box.createRigidArea(new Dimension(200,0)));
		p1.add(print);
		p1.add(estate);
		p1.add(road);
		
		//P2
		p2=new JPanel();
		p2.setPreferredSize(new Dimension(800, 260));
		
		// create blank JTable on screen
		String[] cols={"HOUSE","ROAD","PRODUCT NAME","PRODUCT SUPPLIER","QUANTITY"};
		int numRows=1000;
		model=new DefaultTableModel(numRows,cols.length);
		model.setColumnIdentifiers(cols);
		table = new JTable(model);
		table.setFillsViewportHeight(true);
		table.setAutoCreateColumnsFromModel(false);
		JScrollPane sp=new JScrollPane(table);
		sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		sp.setPreferredSize(new Dimension(790, 250));
				
		//adjust size of columns on table
		TableColumnModel tbm=table.getColumnModel();
		tbm.getColumn(0).setMaxWidth(50);
		tbm.getColumn(4).setMaxWidth(65);
		model.setRowCount(0);
		
		p2.add(sp,BorderLayout.CENTER);
		
		this.add(p1, BorderLayout.NORTH);
		this.add(p2, BorderLayout.SOUTH);
	}
	
	//display orders in table
	public void display()
	{
		ArrayList<Delivery> del=new ArrayList<Delivery>();
		del=eo.getEstateDeliveries(ro.getEstId(estName),getDay(dayPicked));
		model.setRowCount(0);
		String n="";
		int counter=0;
		for(int i=0;i<del.size();i++)
		{
				Vector<Object> row=new Vector<Object>();
				row.add(del.get(i).getHouse());
				n=ro.getRoadName(Integer.parseInt(del.get(i).getRoad()));
				row.add(n);
				row.add(del.get(i).getName());
				row.add(del.get(i).getSupp());
				row.add(del.get(i).getQty());
				model.insertRow(counter,row);
				counter++;
		}
	}
	
	//get day id
	public int getDay(String n)
	{
		int d=0;
		if(n.equals("Monday"))
		{
			d=1;
		}
		else if(n.equals("Tuesday"))
		{
			d=2;
		}
		else if(n.equals("Wednsday"))
		{
			d=3;
		}
		else if(n.equals("Thursday"))
		{
			d=4;
		}
		else if(n.equals("Friday"))
		{
			d=5;
		}
		else if(n.equals("Saturday"))
		{
			d=6;
		}
		else
		{
			d=7;
		}
		return d;
	}

	
	@Override
	public void itemStateChanged(ItemEvent ae) 
	{
		if (ae.getSource() == est && ae.getStateChange() == ItemEvent.SELECTED)
		{
			estName=(String)est.getSelectedItem();
			
		}
		if (ae.getSource() == day && ae.getStateChange() == ItemEvent.SELECTED)
		{
			dayPicked=(String)day.getSelectedItem();
		}
		
	}

	@Override
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==estate)
		{
			String a=JOptionPane.showInputDialog(null,"Enter the name of the new Estate"
					,"New Estate",JOptionPane.QUESTION_MESSAGE);
			String b=JOptionPane.showInputDialog(null,"Enter the delivery charge for the new Estate"
					,"New Estate",JOptionPane.QUESTION_MESSAGE);
			if(a==null||b==null)
			{
				//do nothing
			}
			else if(a.equals("")||b.equals(""))
			{
				JOptionPane.showMessageDialog(null,"You must enter a name and delivery charge for the estate!",
						"ERROR",JOptionPane.WARNING_MESSAGE);
			}
			else
			{
				double db=Double.parseDouble(b);
				eo.addEstate(new Estate(a,db));
				System.out.println("estate added");
				JOptionPane.showMessageDialog(null,"New estate added",
						"CONFIRMATION",JOptionPane.WARNING_MESSAGE);
			}
		}
		else if(ae.getSource()==road)
		{
			String[] list=new String[ests.size()];
			for(int i=0;i<ests.size();i++)
			{
				list[i]=ests.get(i);
			}
			est2 = new JComboBox<String>(list);
			JOptionPane.showMessageDialog( null, est2, "select an estate for the new road",
					JOptionPane.QUESTION_MESSAGE);
			String d=(String) est2.getSelectedItem();
			String c=JOptionPane.showInputDialog(null,"Enter the name of the new road"
					,"New Estate",JOptionPane.QUESTION_MESSAGE);
			if(c==null)
			{
				//do nothing
			}
			else if(c.equals(""))
			{
				JOptionPane.showMessageDialog(null,"You must give the road a name!",
						"ERROR",JOptionPane.WARNING_MESSAGE);
			}
			else
			{
				int eid=ro.getEstId(d);
				ro.addRoad(new Road(eid,c));
				System.out.println("road added");
				JOptionPane.showMessageDialog(null,"New road added",
						"CONFIRMATION",JOptionPane.WARNING_MESSAGE);
			}
		}
		else if(ae.getSource()==show)
		{
			display();
		}
		else //print button
		{
			try {  
				MessageFormat headerFormat = new MessageFormat(estName);
	              MessageFormat footerFormat = new MessageFormat("- {0} -");
	              table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
	            } catch (PrinterException pe) 
	            {
	              System.err.println("Error printing: " + pe.getMessage());
	            }
		}
	}

}
